/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sales.managedbean;

import javax.faces.event.ActionEvent;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import sales.entity.LineItem;
import sales.entity.Product;
import sales.entity.SalesInquiry;
import sales.seesion.stateless.SalesInquirySessionBean;
import sales.util.exception.SalesException;

/**
 *
 * @author frankxgz
 */
@ManagedBean
@ViewScoped
public class CreateNewInquiryManagedBean implements Serializable{
    
    @EJB
    SalesInquirySessionBean inquirySessionBean;
    private Long companyId;
    private Long[] productIds = {Long.valueOf(1L),Long.valueOf(2L),Long.valueOf(3L),Long.valueOf(4L)};
    private int quantity1 = 0;
    private int quantity2 = 0;
    private int quantity3 = 0;
    private int quantity4 = 0;
    
    /** Creates a new instance of CreateInquiryManagedBean */
    public CreateNewInquiryManagedBean() {
    }
    
    public void saveInquiry(ActionEvent event) throws IOException, SalesException{
        String companyName = (String) event.getComponent().getAttributes().get("companySelected");
        companyId = inquirySessionBean.getCompanyByName(companyName).getId();
       
        List productsList = new ArrayList<Integer>();
        List quantitiesList = new ArrayList<Integer>();
        
        if (getQuantity1()!=0){ productsList.add(getProductIds()[0]);quantitiesList.add(getQuantity1());}
        if (getQuantity2()!=0){ productsList.add(getProductIds()[1]);quantitiesList.add(getQuantity2());}
        if (getQuantity3()!=0){ productsList.add(getProductIds()[2]);quantitiesList.add(getQuantity3());}
        if (getQuantity4()!=0){ productsList.add(getProductIds()[3]);quantitiesList.add(getQuantity4());}
        
        SalesInquiry inquiry = inquirySessionBean.addSalesInquiry(getCompanyId(), productsList, quantitiesList);
        
        FacesContext.getCurrentInstance().getExternalContext().getFlash().put("newInquiry", inquiry);
        FacesContext.getCurrentInstance().getExternalContext().redirect("createSalesInquiryResult.xhtml");
    }

    public Long getCompanyId() {
        return companyId;
    }

    public void setCompanyId(Long companyId) {
        this.companyId = companyId;
    }

    public Long[] getProductIds() {
        return productIds;
    }

    public void setProductIds(Long[] productIds) {
        this.productIds = productIds;
    }

    public int getQuantity1() {
        return quantity1;
    }

    public void setQuantity1(int quantity1) {
        this.quantity1 = quantity1;
    }

    public int getQuantity2() {
        return quantity2;
    }

    public void setQuantity2(int quantity2) {
        this.quantity2 = quantity2;
    }

    public int getQuantity3() {
        return quantity3;
    }

    public void setQuantity3(int quantity3) {
        this.quantity3 = quantity3;
    }

    public int getQuantity4() {
        return quantity4;
    }

    public void setQuantity4(int quantity4) {
        this.quantity4 = quantity4;
    }

    
}
